package irio.gamer;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import utils.JDouble;
import utils.JMyCombo;
import utils.JTripleValue;
import utils.Vec;
import Logic.Galaxy;
import Logic.GalaxyComponent;
import Logic.GamerParams;
import Logic.Renderlist;

public class RenderlistPanel extends JPanel{

	
	JLabel lblFilename = new JLabel();
	public static DefaultListModel list = new DefaultListModel();
	JList lst = null;
	public String getLabel() {
		return "Renderlist : " + gamer.gamerParams.renderList;
	}

	public int returnVal;
	
	public void loadRenderlist() {
		final JFileChooser fc = new JFileChooser(gamer.gamerParams.sceneDir + "*.rl");
		fc.setCurrentDirectory(new File(gamer.gamerParams.sceneDir));
		fc.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
			        if (returnVal == JFileChooser.APPROVE_OPTION && fc.getSelectedFile()!=null) {
			        	gamer.gamerParams.renderList = fc.getSelectedFile().getName();
			        	gamer.imagePanel.preview.resetPreview(false);
			    		lblFilename.setText(getLabel());
			   }
			}
		});
       returnVal  = fc.showOpenDialog(gamer.galaxyPanel);
		
	}

	
	
	public JPanel filePanel() {
		JPanel p = new JPanel();
		p.setLayout(new GridLayout(3,1));
		p.add(lblFilename);
		lblFilename.setText(getLabel());
		JButton btnLoad = new JButton("Load");
		p.add(btnLoad);
//		p.add(btnNew);
		
	/*	btnNew.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				String s = (String)JOptionPane.showInputDialog(
				                    gamer.galaxyPanel,
				                    "Enter a galaxy name (without extension)");

				//If a string was returned, say so.
				if ((s != null) && (s.length() > 0)) {
					Galaxy newGalaxy = new Galaxy();
					newGalaxy.initialized = true;
					newGalaxy.Save(s + ".gax");
					gamer.reload( s + ".gax");
				    return;
				}

			}
		});
		
		*/
		btnLoad.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				loadRenderlist();
				//In response to a button click:
				
			}
		});
		
		return p;
	}

//	public String[] galaxies = {"spiral_barred.gax", "spiral_andromeda.gax", "elliptical.gax"};
//	public String[] galaxies = {"elliptical.gax", "andromeda.gax", "hubble1.gax", "SB0.gax", "spiral_barred.gax", "spiral_hubble2.gax"};
	public String[] galaxies = {"andromeda.gax", "SB0.gax", "spiral_barred.gax", "spiral_hubble2.gax"};
	
	public RenderlistPanel() {

//		JMyCombo useGenerated = new JMyCombo("Use renderlist", g.innerTwirl, GamerParams.yesno, GamerParams.yesnoVals);
		setLayout(new GridLayout(3,2));
		add(filePanel());

		JButton btnCreateRenderlist = new JButton("Create!");
		JDouble dblSize = new JDouble("Size", gamer.gamerParams.renderlistSize, new Vec(0,0,0), new Vec(1000,0,0), GamerParams.integer);
		JDouble dblCount = new JDouble("# galaxies", gamer.gamerParams.renderlistCount, new Vec(0,0,0), new Vec(10000,0,0), GamerParams.integer);
		list.clear();
		lst = new JList(list);
		for (int i = 0; i<galaxies.length;i++) {
			list.addElement(galaxies[i]);
		}

		add( new JTripleValue(lst,dblSize, dblCount, btnCreateRenderlist));
		
		
		btnCreateRenderlist.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent arg0) {
				Renderlist.Create(gamer.gamerParams.sceneDir + "random.rl", list);
			}
		});
		
		
	}
	
	
}
